<template>
  <div class="container">
    <el-form :inline="true" :model="search" class="demo-form-inline">
      <el-form-item label="仓库">
      <el-autocomplete
        v-model="wareSign"
        :fetch-suggestions="wareQuerySearchAsync"
        placeholder="请选择仓库标签"
        @select="wareHandleSelect"
      ></el-autocomplete>
    </el-form-item>
      <el-form-item label="货物">
        <el-autocomplete
          v-model="goodsSign"
          :fetch-suggestions="goodsQuerySearchAsync"
          placeholder="请选择货物标签"
          @select="goodsHandleSelect"
        ></el-autocomplete>
      </el-form-item>
<!--      <el-form-item label="供应商">-->
<!--        <el-autocomplete-->
<!--          v-model="supSign"-->
<!--          :fetch-suggestions="supQuerySearchAsync"-->
<!--          placeholder="请选择供应商标签"-->
<!--          @select="supHandleSelect"-->
<!--        ></el-autocomplete>-->
<!--      </el-form-item>-->

      <!--      <el-form-item label="手机号">-->
      <!--        <el-input v-model="search.cusTel" placeholder="请输入手机号"></el-input>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="活动区域">-->
      <!--        <el-select v-model="search.region" placeholder="活动区域">-->
      <!--          <el-option label="区域一" value="shanghai"></el-option>-->
      <!--          <el-option label="区域二" value="beijing"></el-option>-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
      </el-form-item>
    </el-form>
    <!--    <div class="button">-->
    <!--      <el-button @click="onAdd" type="primary">新增供应商</el-button>-->
    <!--    </div>-->
    <el-table
      border
      :data="list"
      style="width: 100%"
      class="table"
    >
      <el-table-column
        prop="wareSign"
        label="仓库"
        width="300">
      </el-table-column>
      <el-table-column
        prop="goodsSign"
        label="货物"
        width="300">
      </el-table-column>
      <el-table-column
        prop="inGoodsPrice"
        label="进价"
        width="200">
      </el-table-column>
      <el-table-column
        prop="inGoodsCount"
        label="货物总量"
        width="200">
      </el-table-column>
      <el-table-column
        prop="inGoodsCost"
        label="货物占位">
      </el-table-column>
      <el-table-column
        prop="inGoodsAmount"
        label="所占货架数">
      </el-table-column>
    </el-table>
    <div style="display: flex; justify-content: flex-end; margin-top: 20px">
      <el-pagination
        v-show="total>0"
        :total="total"
        background
        @current-change="getList"
        :current-page.sync="search.currentPage"
        :page-size.sync="search.size"
        layout="total, prev, pager, next"
      />
    </div>
  </div>

</template>

<script>
import {wareSignList, goodsSignList, inventoryList} from '@/api/user'
export default {
  name: 'InventoryInfo',
  data() {
    return {
      search: {
        goodsId: '',
        wareId: '',
        currentPage: 1,
        size: 10
      },
      total: 0,
      list: [],
      timeout: null,
      goodsSign: '',
      goodsSigns: [],
      wareSign: '',
      wareSigns: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.search.goodsId = this.goodsSign.length == 0 ? '' : this.search.goodsId
      this.search.wareId = this.wareSign.length == 0 ? '' : this.search.wareId
      inventoryList(this.search).then(res => {
        console.log(this.search)
        this.list = res.data.list
        this.total = res.data.total
      })
    },
    loadAll() {
      wareSignList().then(res =>{
        this.wareSigns = res.data
        console.log(this.wareSigns)
      })
      goodsSignList().then(res =>{
        this.goodsSigns = res.data
        console.log(this.goodsSigns)
      })
    },
    goodsQuerySearchAsync(queryString, cb) {
      var goodsSigns = this.goodsSigns
      console.log(goodsSigns)
      var results = queryString ? goodsSigns.filter(this.createStateFilter(queryString)) : goodsSigns
      console.log(results)
      clearTimeout(this.timeout)
      this.timeout = setTimeout(() => {
        cb(results)
      }, 1000 * Math.random())
    },
    wareQuerySearchAsync(queryString, cb) {
      var wareSigns = this.wareSigns
      console.log(wareSigns)
      var results = queryString ? wareSigns.filter(this.createStateFilter(queryString)) : wareSigns
      console.log(results)
      clearTimeout(this.timeout)
      this.timeout = setTimeout(() => {
        cb(results)
      }, 1000 * Math.random())
    },
    createStateFilter(queryString) {
      return (state) => {
        return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    },
    goodsHandleSelect(item) {
      console.log(item)
      this.search.goodsId = item.goodsId
      console.log(this.goodsId)
    },
    wareHandleSelect(item) {
      console.log(item)
      this.search.wareId = item.wareId
      console.log(this.wareId)
    }
  },
  mounted() {
    this.loadAll()
  }
}
</script>

<style scoped>
.container {
  padding: 50px;
  display: flex;
  flex-direction: column;
.button {
  display: flex;
  justify-content: flex-end;
}
.table {
  margin-top: 20px;
}
}
</style>
